// LightSourcesDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CGWork.h"
#include "LightSourcesDlg.h"


// LightSourcesDlg dialog

std::vector<light_t> LightSourcesDlg::m_lights = std::vector<light_t>(num_of_lights);
bool LightSourcesDlg::lights_init = true;

IMPLEMENT_DYNAMIC(LightSourcesDlg, CDialog)

LightSourcesDlg::LightSourcesDlg(CWnd* pParent /*=NULL*/)
	: CDialog(LightSourcesDlg::IDD, pParent)
	, m_ambient_r(255)
	, m_ambient_g(255)
	, m_ambient_b(255)
	, m_light_parallel(false)
	, m_light_point(false)
	, m_light_r(0)
	, m_light_g(0)
	, m_light_b(0)
	, m_light_x(0)
	, m_light_y(0)
	, m_light_z(0)
	, m_light_enable(FALSE)
	, m_is_parallel(FALSE)
{
	if (lights_init) {
		m_lights[0].enabled = true;
		lights_init = false;
	}

	for (unsigned i(0) ; i<num_of_lights ; ++i)
		m_lights_edited[i] = m_lights[i];
}

LightSourcesDlg::~LightSourcesDlg()
{
}

void LightSourcesDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_AMBIENT_R, m_ambient_r);
	DDV_MinMaxInt(pDX, m_ambient_r, 0, 255);
	DDX_Text(pDX, IDC_AMBIENT_G, m_ambient_g);
	DDV_MinMaxInt(pDX, m_ambient_g, 0, 255);
	DDX_Text(pDX, IDC_AMBIENT_B, m_ambient_b);
	DDV_MinMaxInt(pDX, m_ambient_b, 0, 255);
	DDX_Control(pDX, IDC_LIGHT_LIST, m_c_lightlist);
	DDX_Text(pDX, IDC_LIGHT_R, m_light_r);
	DDV_MinMaxInt(pDX, m_light_r, 0, 255);
	DDX_Text(pDX, IDC_LIGHT_G, m_light_g);
	DDV_MinMaxInt(pDX, m_light_g, 0, 255);
	DDX_Text(pDX, IDC_LIGHT_B, m_light_b);
	DDV_MinMaxInt(pDX, m_light_b, 0, 255);
	DDX_Text(pDX, IDC_LIGHT_X, m_light_x);
	DDX_Text(pDX, IDC_LIGHT_Y, m_light_y);
	DDX_Text(pDX, IDC_LIGHT_Z, m_light_z);
	DDX_Check(pDX, IDC_LIGHT_ENABLE, m_light_enable);
	DDX_Radio(pDX, IDC_PARALLEL_LIGHT, m_is_parallel);
}


BEGIN_MESSAGE_MAP(LightSourcesDlg, CDialog)
	ON_BN_CLICKED(IDOK, &LightSourcesDlg::OnBnClickedOk)
	ON_LBN_SELCHANGE(IDC_LIGHT_LIST, &LightSourcesDlg::OnLbnSelchangeLightList)
	ON_BN_CLICKED(IDC_PARALLEL_LIGHT, &LightSourcesDlg::OnBnClickedParallelLight)
	ON_BN_CLICKED(IDC_POINT_LIGHT, &LightSourcesDlg::OnBnClickedPointLight)
	ON_EN_CHANGE(IDC_LIGHT_R, &LightSourcesDlg::OnEnChangeLightR)
	ON_EN_CHANGE(IDC_LIGHT_G, &LightSourcesDlg::OnEnChangeLightG)
	ON_EN_CHANGE(IDC_LIGHT_B, &LightSourcesDlg::OnEnChangeLightB)
	ON_EN_CHANGE(IDC_LIGHT_X, &LightSourcesDlg::OnEnChangeLightX)
	ON_EN_CHANGE(IDC_LIGHT_Y, &LightSourcesDlg::OnEnChangeLightY)
	ON_EN_CHANGE(IDC_LIGHT_Z, &LightSourcesDlg::OnEnChangeLightZ)
	ON_BN_CLICKED(IDC_LIGHT_ENABLE, &LightSourcesDlg::OnBnClickedLightEnable)
END_MESSAGE_MAP()


// LightSourcesDlg message handlers

void LightSourcesDlg::OnBnClickedOk()
{
	// we shall copy all the data from edited lights to normal lights
	for (unsigned i(0) ; i<num_of_lights ; ++i)
		m_lights[i] = m_lights_edited[i];

	// TODO: Add your control notification handler code here
	OnOK();
}

BOOL LightSourcesDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	for (unsigned i(0) ; i<num_of_lights ; ++i) {
		CString name;
		name.Format("light %d", i);
		m_c_lightlist.AddString(name);
	}

	m_c_lightlist.SetCurSel(0);

	ShowData();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void LightSourcesDlg::ShowData () {
	UpdateData(TRUE);
	int light_index = m_c_lightlist.GetCurSel();
	m_light_parallel = m_lights_edited[light_index].parallel;
	m_light_point = m_lights_edited[light_index].point;
	m_is_parallel = !m_light_parallel;
	m_light_r = m_lights_edited[light_index].r;
	m_light_g = m_lights_edited[light_index].g;
	m_light_b = m_lights_edited[light_index].b;
	m_light_x = m_lights_edited[light_index].x;
	m_light_y = m_lights_edited[light_index].y;
	m_light_z = m_lights_edited[light_index].z;
	m_light_enable = m_lights_edited[light_index].enabled;
	UpdateData(FALSE);
}

void LightSourcesDlg::OnLbnSelchangeLightList()
{
	// TODO: Add your control notification handler code here
	ShowData();
}

void LightSourcesDlg::ReadData () {
	UpdateData(TRUE);
	int light_index = m_c_lightlist.GetCurSel();

	m_lights_edited[light_index].parallel = !(m_is_parallel == TRUE);
	m_lights_edited[light_index].point = m_is_parallel == TRUE;
	m_lights_edited[light_index].r = m_light_r;
	m_lights_edited[light_index].g = m_light_g;
	m_lights_edited[light_index].b = m_light_b;
	m_lights_edited[light_index].x = m_light_x;
	m_lights_edited[light_index].y = m_light_y;
	m_lights_edited[light_index].z = m_light_z;
	m_lights_edited[light_index].enabled = m_light_enable;

	UpdateData(FALSE);
}

void LightSourcesDlg::OnBnClickedParallelLight()
{
	ReadData();
}

void LightSourcesDlg::OnBnClickedPointLight()
{
	ReadData();
}

void LightSourcesDlg::OnEnChangeLightR()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	ReadData();
}

void LightSourcesDlg::OnEnChangeLightG()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	ReadData();
}

void LightSourcesDlg::OnEnChangeLightB()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	ReadData();
}

void LightSourcesDlg::OnEnChangeLightX()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	ReadData();
}

void LightSourcesDlg::OnEnChangeLightY()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	ReadData();
}

void LightSourcesDlg::OnEnChangeLightZ()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	ReadData();
}

void LightSourcesDlg::OnBnClickedLightEnable()
{
	ReadData();
}
